﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Useful
{
    public class MessageBoxEx : Form
    {
        private int ResultButton { get; set; }

        private MessageBoxEx(string message, string header, params string[] buttons)
        {
            ResultButton = -1;

            this.Text = header;
            this.AutoSize = true;
            this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.FormBorderStyle = FormBorderStyle.Fixed3D;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.StartPosition = FormStartPosition.CenterParent;

            TableLayoutPanel panel = new TableLayoutPanel();
            panel.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
            panel.Dock = DockStyle.Fill;
            //panel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            panel.AutoSize = true;
            panel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            this.Controls.Add(panel);

            Label l = new Label();
            l.AutoSize = true;
            l.Text = message;
            l.TextAlign = ContentAlignment.MiddleCenter;
            l.Dock = DockStyle.Fill;
            l.Padding = new Padding(5);
            panel.Controls.Add(l);

            TableLayoutPanel bPanel = new TableLayoutPanel();
            bPanel.Dock = DockStyle.Fill;
            //bPanel.CellBorderStyle = TableLayoutPanelCellBorderStyle.Single;
            bPanel.AutoSize = true;
            bPanel.AutoSizeMode = AutoSizeMode.GrowAndShrink;
            bPanel.Padding = new Padding(5);
            panel.Controls.Add(bPanel);

            for(int i = 0; i< buttons.Length; i++)
            {
                Button btn = new Button();
                btn.Text = buttons[i];
                btn.AutoSize = false;
                btn.Tag = i + 1;
                btn.Dock = DockStyle.Fill;
                btn.Click += btn_Click;

                bPanel.Controls.Add(btn, i, 0);
                bPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100.0F / buttons.Length));
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;

            ResultButton = (int)btn.Tag;

            Close();
        }

        public static int ShowDialog(string message, string header, params string[] buttons)
        {
            MessageBoxEx frm = new MessageBoxEx(message, header, buttons);

            frm.ShowDialog();

            return frm.ResultButton;
        }
    }
}
